﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

namespace EHTWebsite.Code
{
    /// <summary>
    /// Author: toanvt
    /// CreatedDate: 12/12/2011
    /// Description: provider Khách Hàng, Đối tác
    /// </summary>
    public class KhachHangProvider
    {
        /// <summary>
        /// Author: toanvt
        /// CreatedDate: 12/12/2011
        /// Description: Insert, Update Khách Hàng
        /// </summary>
        /// <param name="theKhachHang">Đối tượng khách hàng</param>
        /// <param name="IsAdd">Thêm mới</param>
        public static void IUKhachHang(KhachHang theKhachHang, bool IsAdd)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_IUKhachHang";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@KhachHangID", theKhachHang.KhachHangID));
            command.Parameters.Add(new SqlParameter("@TenKhachHangVN", theKhachHang.TenKhachHangVN));
            command.Parameters.Add(new SqlParameter("@TenKhachHangOther", theKhachHang.TenKhachHangOther));
            command.Parameters.Add(new SqlParameter("@NgaySinh", theKhachHang.NgaySinh));
            command.Parameters.Add(new SqlParameter("@DiaChiVN", theKhachHang.DiaChiVN));
            command.Parameters.Add(new SqlParameter("@DiaChiOther", theKhachHang.DiaChiOther));
            command.Parameters.Add(new SqlParameter("@DienThoai", theKhachHang.DienThoai));
            command.Parameters.Add(new SqlParameter("@DiDong", theKhachHang.DiDong));
            command.Parameters.Add(new SqlParameter("@Email", theKhachHang.Email));
            command.Parameters.Add(new SqlParameter("@HinhAnhUrl", theKhachHang.HinhAnhUrl));
            command.Parameters.Add(new SqlParameter("@GhichuVN", theKhachHang.GhichuVN));
            command.Parameters.Add(new SqlParameter("@GhiChuOther", theKhachHang.GhiChuOther));
            command.Parameters.Add(new SqlParameter("@Website", theKhachHang.Website));
            command.Parameters.Add(new SqlParameter("@NhomKhachHang",theKhachHang.NhomKhachHang));
            command.Parameters.Add(new SqlParameter("@Publish", theKhachHang.Publish));
            command.Parameters.Add(new SqlParameter("@NumberOfOrder",theKhachHang.NumberOfOrder));
            command.Parameters.Add(new SqlParameter("@IsAdd", IsAdd));

            DataProvider.ExecuteNoneQuery(command);
        }

        /// <summary>
        /// Author: toanvt
        /// CreatedDate: 12/12/2011
        /// Description: Xóa Khách Hàng
        /// </summary>
        /// <param name="KhachHangID">Mã khách hàng</param>
        public static void DeleteKhachHangByKhachHangID(Int32 KhachHangID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_DeleteKhachHangByKhachHangID";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@KhachHangID", KhachHangID));

            DataProvider.ExecuteNoneQuery(command);
        }

        /// <summary>
        /// Author: toanvt
        /// CreatedDate: 12/12/2011
        /// Description: Lấy danh sách Khách Hàng có phân trang
        /// </summary>
        /// <param name="theKhachHang">Đối tượng khách hàng chứa các khóa lọc</param>
        /// <param name="pageIndex">Trang hiện tại</param>
        /// <param name="pageSize">Số record trên 1 trang</param>
        /// <param name="totalRecord">Tông số record tìm thấy</param>
        /// <returns>Danh sách Khách Hàng</returns>
        public static DataSet GetKhachHangsByFillter(KhachHang theKhachHang, Int32 pageIndex, Int32 pageSize, out Int32 totalRecord)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_GetKhachHangsByFillter";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@KhachHangID", theKhachHang.KhachHangID));
            command.Parameters.Add(new SqlParameter("@TenKhachHangVN", theKhachHang.TenKhachHangVN));
            command.Parameters.Add(new SqlParameter("@TenKhachHangOther", theKhachHang.TenKhachHangOther));
            command.Parameters.Add(new SqlParameter("@DienThoai", theKhachHang.DienThoai));
            command.Parameters.Add(new SqlParameter("@Email", theKhachHang.Email));
            command.Parameters.Add(new SqlParameter("@NhomKhachHang",theKhachHang.NhomKhachHang));
            command.Parameters.Add(new SqlParameter("@Publish",theKhachHang.Publish));
            command.Parameters.Add(new SqlParameter("@NumberOfOrder",theKhachHang.NumberOfOrder));
            command.Parameters.Add(new SqlParameter("@PageIndex", pageIndex));
            command.Parameters.Add(new SqlParameter("@PageSize", pageSize));
            DataSet ds = DataProvider.ExecuteDataSet(command, out totalRecord);
            return ds;
        }

        /// <summary>
        /// Author: toanvt
        /// CreatedDate: 12/12/2011
        /// Description: Lấy đối tượng Khách Hàng bằng Mã khách hàng
        /// </summary>
        /// <param name="KhachHangID">Mã khách hàng</param>
        /// <returns>Dối tượng Khách Hàng</returns>
        public static KhachHang GetKhachHangByKhachHangID(Int32 KhachHangID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_GetKhachHangByKhachHangID";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@KhachHangID", KhachHangID));

            DataSet ds = DataProvider.ExecuteDataSet(command);
            KhachHang theKhachHangNew = null;

            if (ds.Tables[0].Rows.Count > 0)
            {
                theKhachHangNew = new KhachHang();
                theKhachHangNew.TenKhachHangVN = ds.Tables[0].Rows[0]["TenKhachHangVN"].ToString();
                theKhachHangNew.TenKhachHangOther = ds.Tables[0].Rows[0]["TenKhachHangOther"].ToString();

                try
                {
                    theKhachHangNew.NgaySinh = DateTime.Parse(ds.Tables[0].Rows[0]["NgaySinh"].ToString());
                }
                catch (Exception)
                {
                }

                theKhachHangNew.DiaChiVN = ds.Tables[0].Rows[0]["DiaChiVN"].ToString();
                theKhachHangNew.DiaChiOther = ds.Tables[0].Rows[0]["DiaChiOther"].ToString();
                theKhachHangNew.DienThoai = ds.Tables[0].Rows[0]["DienThoai"].ToString();
                theKhachHangNew.DiDong = ds.Tables[0].Rows[0]["DiDong"].ToString();
                theKhachHangNew.Email = ds.Tables[0].Rows[0]["Email"].ToString();
                theKhachHangNew.GhichuVN = ds.Tables[0].Rows[0]["GhichuVN"].ToString();
                theKhachHangNew.GhiChuOther = ds.Tables[0].Rows[0]["GhiChuOther"].ToString();
                theKhachHangNew.HinhAnhUrl = ds.Tables[0].Rows[0]["HinhAnhUrl"].ToString();
                theKhachHangNew.Website = ds.Tables[0].Rows[0]["Website"].ToString();
                theKhachHangNew.NhomKhachHang = Int32.Parse(ds.Tables[0].Rows[0]["NhomKhachHang"].ToString());
                theKhachHangNew.Publish = Boolean.Parse(ds.Tables[0].Rows[0]["Publish"].ToString());
                theKhachHangNew.NumberOfOrder = Int32.Parse(ds.Tables[0].Rows[0]["NumberOfOrder"].ToString());
                theKhachHangNew.KhachHangID = Int32.Parse(ds.Tables[0].Rows[0]["KhachHangID"].ToString());
            }

            return theKhachHangNew;
        }
        public static DataSet UpdateNumberOfOrderByKhachHangId(Int32 KhachHangID, Int32 NumberOfOrder)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_UpdateKhachHangNumberOfOrderByKhachHangId";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@KhachHangID",KhachHangID));
            command.Parameters.Add(new SqlParameter("@NumberOfOrder",NumberOfOrder));
            return DataProvider.ExecuteDataSet(command);
        }
        public static void ChangePublishByDanhMucKhachHangID(Int32 KhachHangID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_ChangePublishByKhachHangID";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@KhachHangID",KhachHangID));
            DataProvider.ExecuteNoneQuery(command);
        }
    }
}